@using Spectre.Console
@inject NavigationManager Nav

<Select Options="@(_tabs.Select(t => t.Label).ToArray())"
    Value="@SelectedTab"
    ValueChanged="@SelectTab"
    IsFocused="@IsFocused"
    IsFocusedChanged="@OnSelectFocusChanged"
    Expand="false"/>

@code {
    private string SelectedTab = "Align";
    private readonly (string Key, string Label)[] _tabs = new[]
    {
        ("align", "Align"),
        ("border", "Border"),
        ("columns", "Columns"),
        ("figlet", "Figlet"),
        ("grid", "Grid"),
        ("table", "Table"),
        ("html-inline", "HTML Inline"),
        ("html-list", "HTML List"),
        ("markup", "Markup"),
        ("markdown", "Markdown"),
        ("syntax", "Syntax Highlight"),
        ("newline", "Newline"),
        ("canvas", "Spectre Canvas"),
        ("padder", "Padder"),
        ("panel", "Panel"),
        ("rows", "Rows"),
        ("select", "Select"),
        ("spinner", "Spinner"),
        ("scrollable", "Scrollable"),
        ("textbutton", "Text Button"),
        ("textinput", "Text Input"),
        ("cli-info", "CLI Info"),
    };

    private void SelectTab(string tabName)
    {
        var key = _tabs.FirstOrDefault(t => t.Label == tabName).Key;
        if (key == null)
        {
            throw new InvalidOperationException($"Tab '{tabName}' not found.");
        }
        SelectedTab = tabName;
        Nav.NavigateTo(key);
    }

    [Parameter]
    public bool IsFocused { get; set; }

    [Parameter]
    public EventCallback<bool> IsFocusedChanged { get; set; }

    private async Task OnSelectFocusChanged(bool isFocused)
    {
        if (IsFocused == isFocused)
        {
            return;
        }

        IsFocused = isFocused;

        if (IsFocusedChanged.HasDelegate)
        {
            await IsFocusedChanged.InvokeAsync(isFocused).ConfigureAwait(false);
        }

        await InvokeAsync(StateHasChanged).ConfigureAwait(false);
    }

}
